package com.rq.adapter.ui;

import android.content.Context;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rq.adapter.LOG;
import com.rq.rvlibrary.ActionPasser;
import com.rq.rvlibrary.AdapterHolderControllerHelper;
import com.rq.rvlibrary.AdapterUseException;
import com.rq.rvlibrary.BaseViewHolder;
import com.rq.rvlibrary.Compare;
import com.rq.rvlibrary.EasyViewHolder;
import com.rq.rvlibrary.NestingBuilder;
import com.rq.rvlibrary.OnClickMaker;
import com.rq.rvlibrary.OnContentKeeper;
import com.rq.rvlibrary.OnInterceptClick;
import com.rq.rvlibrary.OnItemClickListener;
import com.rq.rvlibrary.OnItemPressListener;
import com.rq.rvlibrary.OnPressMaker;
import com.rq.rvlibrary.RQDiffUtil;
import com.rq.rvlibrary.ViewDataGetter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BaseAdapter2<DATA, VH extends BaseViewHolder> extends
        RecyclerView.Adapter<VH> implements
        AdapterHolderControllerHelper {
    protected SparseArray<Class<? extends BaseViewHolder>> multipleHolder;
    Context mContext;
    Class<VH> mHolder;//内部不要直接引用 避免出错  ->  mainHolder()
    private int itemId;
    private Object mObject;
    private List<DATA> showData = new ArrayList<>();
    private SparseArray<Class<? extends BaseViewHolder>> headType = new SparseArray<>();//顶部视图处理器类型
    private SparseArray<Object> headViewData = new SparseArray<>();//顶部视图数据
    private SparseIntArray headViewResId = new SparseIntArray();
    private SparseArray<Class<? extends BaseViewHolder>> footType = new SparseArray<>();//底部视图处理器类型
    private SparseArray<Object> footViewData = new SparseArray<>();//底部视图数据
    private SparseIntArray footViewResId = new SparseIntArray();
    private ActionPasser mActionPasser;
    private HashMap<Class, OnClickMaker> clicks = new HashMap<>();
    private HashMap<Class, OnPressMaker> press = new HashMap<>();
    //  private OnAttachedToBottomListener mOnAttachedToBottomListener;
    private Map<String, Object> contentCash = new HashMap<>();

    private void setContext(Context context) {
        this.mContext = context;
    }

    /**
     * 默认 EasyViewHolder.class
     */
    public BaseAdapter2(Context context, @LayoutRes int itemLayoutId) {
        this.mContext = context;
        this.itemId = itemLayoutId;
        this.mObject = null;
    }

    boolean showEmpty = false;//展示空视图
    SparseArray<Object> pass;

    @Deprecated
    public void setPassData(Object... passData) {
        if (passData != null && passData.length > 0) {
            if (pass == null) {
                pass = new SparseArray<>();
            }
            for (int i = 0; i < passData.length; i++) {
                pass.put(i, passData[i]);
            }
        }
    }


    /**
     * 直接刷新视图，数据为空将置空列表
     *
     * @param dataList 填充数据
     */
    public synchronized void setData(List dataList) {
        this.showData.clear();
        if (dataList != null) {
            this.showData = dataList;
        }
        this.notifyChange();
    }


    @Override
    public List getData() {
        return this.showData;
    }

    @Override
    public void notifyChange() {
        LOG.showUserWhere("notifyChange");
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    private Class mainHolder() {
        if (mHolder != null) {
            return mHolder;
        } else {
            return EasyViewHolder.class;
        }
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(itemId, parent, false);
        itemView.setTag(com.rq.rvlibrary.R.id.rv_child_layout, itemId);
        return (VH) new EasyViewHolder<>(itemView);
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        Object data = showData.get(position);
        onBindEasyHolder(this, holder, position - headViewData.size(), (DATA) data);
    }

    private int getCSPosition(int position) {
        if (position < headViewData.size()) {
            return headViewData.size() * -1 + position;
        } else if (position >= headViewData.size() + showData.size() && headViewData.size() + showData
                .size() > 0) {
            int positionInFoot = position - headViewData.size() - showData.size();
            return Integer.MIN_VALUE + positionInFoot;
        } else {
            return position - headViewData.size();
        }
    }

    protected void onBindEasyHolder(BaseAdapter2 adapter, VH holder, int position, DATA data) {
    }

    @Override
    @Deprecated
    public int getItemCount() {
        int size = 0;
        if (showData != null) {
            size += showData.size();
        }
        return size;
    }


    /**
     * 常见交互可通过 addOnItemClickListener 方法设置
     *
     */
    @Deprecated
    public void setActionPasser(ActionPasser passer) {
        this.mActionPasser = passer;
    }


    public void moveItem(int fromPosition, int toPosition) {
        Collections.swap(showData, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }
}
